home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Lightspeed C SKEL fldr / Skel setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-17  |  5.5 KB  |  192 lines  |  [TEXT/KAHL]

  1. #include <WindowMgr.h>        
  2. #include <MenuMgr.h>
  3. #include <EventMgr.h>
  4. #include "Skel defines.h"
  5. #include "Skel globals.h"
  6.  
  7. #include <MemoryMgr.h>        
  8.  
  9. /*
  10.  
  11. SetUps for handling memory
  12. ##########################   SetUpMemory   ############################
  13.      This very important set of initializations can be left out of the
  14.    first versions of a program. We are making sure that memory is laid
  15.    out as we desire, with adequate protection against running out of
  16.    memory, bad handles, etc.
  17. */
  18.  
  19. setupmemory () {
  20.  
  21.  
  22. #define maxStackSize 8192    /* max size of stack; the heap gets the
  23.                    rest */
  24.  
  25.     typedef long   *lomemptr;    /* a pointer to low memory locations */
  26.  
  27.     lomemptr nilptr;        /* will have value NIL */
  28.     lomemptr stackbaseptr;    /* points to current stack base */
  29.  
  30. /* 
  31.   If you define a GrowZone function to Handle bad memory problems,
  32.   you should define it at the top level (not nested), and set it
  33.   here. We don't.
  34. */
  35. /*     SetGrowZone(&mygrowzone);   
  36. */
  37. /* 
  38.   Place a longint -1 (an odd and therefore illegal address) in the
  39.   memory location that would be referenced by an accidentally-NULL
  40.   Handle, so the error will be caught at Handle-reference time (as
  41.   an Address error, ID=02) instead of later on.
  42. */
  43.  
  44.     nilptr = 0l;
  45.     *nilptr = (long)-1;
  46.  
  47. /* 
  48.   If you needed to use an Application heap limit other than the
  49.   default (which allows 8K for the stack), you'd set it here,
  50.   possible using this technique of explicitly specifying the maximum
  51.   stack size and allocating the rest to the heap.  Should be
  52.   independent of memory size. */
  53.  
  54.     stackbaseptr = (lomemptr) 0x908;
  55.                 /* CurStackBase from Tlasm/sysequ.text */
  56.     SetApplLimit ((Ptr) (*stackbaseptr - maxStackSize));
  57.  
  58. /* 
  59.   Expand the application heap Zone to its maximum size, without
  60.   purging any purgeable resources.  This saves memory compactions
  61.   and heap expansions later.
  62. */
  63.  
  64.     MaxApplZone ();
  65.  
  66. /* 
  67.   get plenty of master pointers now; if we let the Memory Manager
  68.   Allocate them as needed, they'd form non-relocatable islands in
  69.   the heap.
  70. */
  71.  
  72.     MoreMasters ();
  73.     MoreMasters ();
  74.     MoreMasters ();
  75.  
  76. /* 
  77.   Here you might install bulwarks against running out of memory
  78.   unexpectedly.  One such (cheesy) technique is to here Allocate
  79.   a large Handle, call it "CheeseBuf", which you can de-Allocate
  80.   in your GrowZone function, when you must obtain more memory to
  81.   avoid a crash.  While de-allocated, the program could prevent
  82.   the user from doing anything requiring memory, and tell him he
  83.   must discard windows or some such memory freeing action. Each
  84.   time he does so, the program can try to re-Allocate CheeseBuf;
  85.   if it succeeds, the user can go on doing memory-eating operations.
  86. */
  87.  
  88. }                /* SetUpMemory */
  89.  
  90. /*
  91. ############################   SetUpMenus   #############################
  92.  
  93.     Once-only initialization for menus
  94.     We read in all menus from the resource file, and install them,
  95.     and all desk accessories (drivers).
  96.     
  97. */
  98.  
  99.  
  100.  
  101. setupmenus () {
  102.     int     i;
  103.  
  104.     for (i = 2; i <= lastmenu; i++)        /* get all my menus in */
  105.     mymenus[i] = GetMenu (i);            /* use the fact that our 
  106.                                               menu ID's start at 1 */
  107.     mymenus[applemenu] = NewMenu(applemenu, "\p\024" );
  108.     AddResMenu (mymenus[applemenu], 'DRVR');
  109.                 /* pull in all desk accessories */
  110.     for (i = 1; i <= lastmenu; i++) InsertMenu (mymenus[i], 0);
  111.     DrawMenuBar ();
  112. }
  113.  
  114. /*
  115. body of SetUp
  116. Once-only initialization for Skel
  117. ############################   SetUp   ##############################
  118.  
  119.    Initialize our program.  It seems best to Handle:
  120.    Memory inits first, ToolBox inits second, then the program variables'
  121.    inits. Note that the order of inits is important; see "Using the
  122.    Dialog Manager" in the Dialog Mgr section.
  123.  
  124. */
  125.  
  126. setup () {
  127. /* 
  128.   The following function is not declared in <win.h>, although
  129.   that is where you would expect it.  - WHJ 3/10/85
  130. */
  131.  
  132.     Rect screenrect;        
  133.     
  134.     setupmemory ();    /*  init memory layout and protection */
  135.  
  136. /*  init QuickDraw, and everybody else */
  137.  
  138.     InitGraf(&thePort);
  139.     InitFonts();
  140.     FlushEvents( everyEvent, 0 );
  141.     InitWindows();
  142.     InitMenus();
  143.     TEInit();
  144.     InitDialogs(0L);
  145.     InitCursor();
  146.  
  147. /* 
  148.   Init the system event mask, in case the previous program left
  149.   it in a bad state.  If you set it non-standard here, FIX IT
  150.   BEFORE EXITING, because the Finder (1.1g) does NOT set it.
  151. */
  152. /*    SetEventMask (everyEvent - keyUpMask);*//* standard setting */
  153. /* 
  154.   Get the port which is the whole screen, to use when deactivating
  155.   our window.  This prevents the current GrafPort pointer from
  156.   ever dangling.
  157. */
  158.     GetWMgrPort (&screenport);    /* get whole screen port that window mgr
  159.                    uses */
  160.     SetPort (screenport);    /* and start off with it */
  161.  
  162. /* 
  163.   get window: use wRecord storage.  Port is set to that of the
  164.   new window.
  165.   GetNewWindow posts an update event for the new window,
  166.   so it will be redrawn right away.
  167. */
  168.     mywindow = GetNewWindow (windowid, &wrecord, (long) - 1);
  169.                 /* -1 => frontmost window */
  170.  
  171. /* set up dragRect; we can drag the window within it */
  172.     screenrect.top = screenBits.bounds.top;
  173.                 /* don't assume screen size */
  174.     screenrect.left = screenBits.bounds.left;
  175.     screenrect.bottom = screenBits.bounds.bottom;
  176.     screenrect.right = screenBits.bounds.right;
  177.  
  178. /* 
  179.   set drag Rect to avoid menu bar, and keep at least 4 pixels
  180.   on screen
  181. */
  182.     SetRect (&dragrect, 4, 24, screenrect.right - 4, screenrect.bottom - 4);
  183.  
  184. /* set up GrowRect, for limits on window growing */
  185.     SetRect (&growrect, 48, 14, screenrect.right - 7, screenrect.bottom - 7);
  186.  
  187. /* pull in and set up our menus */
  188.     setupmenus ();
  189.  
  190. }
  191.  
  192.